<template>
  <div :style="{ ...coreStyle.marginSame(8) }">
    <a-row :gutter="[8, 8]">
      <a-col :span="8">
        <a-input-search v-model:value="projectModuleFormQueryModel.keySearchVo" placeholder="关键字搜索" :disabled="projectModuleFormQuerySubmitting" @search="projectModuleFormQuerySearch" />
        <a-button type="dashed" block :style="{ ...coreStyle.height(272), ...coreStyle.marginTop(8) }" @click="projectModuleAdd">
          <CoreIcon icon="carbon:add-alt" :size="14" />
          新增项目模块
        </a-button>
      </a-col>
      <a-col :span="8" v-for="item in projectModuleListDataSet">
        <a-card :title="`${item.projectModuleNotes} - ${item.projectModuleName}`" hoverable>
          <template #extra>
            <a :style="{ ...coreStyle.marginRight(8) }" @click="projectModuleUpdate(item.id)">
              <CoreIcon icon="ri:brush-line" color="#f90" :size="22" />
            </a>
            <a-popconfirm title="确认删除？" placement="bottom" @confirm="projectModuleDelete(item.id)">
              <CoreIcon icon="fluent:delete-28-regular" color="#ed4014" :size="22" />
            </a-popconfirm>
          </template>
          <div :style="{ ...coreStyle.height(157) }">
            <ScrollContainer>
              <a-descriptions size="small" :column="2" bordered>
                <a-descriptions-item label="代码缩进" :span="1">{{ getCodeCiDisplayName(item.codeCi) }}</a-descriptions-item>
                <a-descriptions-item label="排序权重" :span="1">{{ item.weightOrder }}</a-descriptions-item>
                <a-descriptions-item label="数据库类型" :span="1">{{ getDbTypeDisplayName(item.dbType) }}</a-descriptions-item>
                <a-descriptions-item label="数据库名" :span="1" v-if="item.dbType === 'mysql'">{{ item.dbName }}</a-descriptions-item>
                <a-descriptions-item label="服务名" :span="1" v-if="item.dbType === 'oracle'">{{ item.serverName }}</a-descriptions-item>
                <a-descriptions-item label="IP地址" :span="1">{{ item.ipAddress }}</a-descriptions-item>
                <a-descriptions-item label="端口" :span="1">{{ item.port }}</a-descriptions-item>
                <a-descriptions-item label="用户名" :span="1">{{ item.username }}</a-descriptions-item>
                <a-descriptions-item label="密码" :span="1">{{ item.password }}</a-descriptions-item>
              </a-descriptions>
              <a-descriptions size="small" :column="1" layout="vertical" bordered>
                <a-descriptions-item label="项目路径" :span="1">{{ item.projectPath }}</a-descriptions-item>
                <a-descriptions-item label="vue项目路径" :span="1">{{ item.projectPathVue }}</a-descriptions-item>
                <a-descriptions-item label="uni项目路径" :span="1">{{ item.projectPathUni }}</a-descriptions-item>
              </a-descriptions>
            </ScrollContainer>
          </div>
          <template #actions>
            <a-button type="primary" size="small" ghost @click="toAddDataSource(item.id)">
              <CoreIcon icon="la:bug" :size="13" />
              数据源
            </a-button>
            <a-button size="small" danger @click="configureDbTables(item.id)">
              <CoreIcon icon="cil:aperture" :size="13" />
              数据库表
            </a-button>
            <a-button type="dashed" size="small" danger @click="codeGenerate(item.id)">
              <CoreIcon icon="whh:lightning" :size="13" />
              代码生成
            </a-button>
          </template>
        </a-card>
      </a-col>
    </a-row>
    <a-drawer
      v-model:visible="projectModuleEditDrawerVisible"
      title="编辑项目模块信息"
      placement="right"
      :closable="false"
      :width="720"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
      @close="projectModuleEditDrawerOnClose"
    >
      <a-form ref="projectModuleFormRef" :model="projectModuleFormModel" :rules="projectModuleFormRules" labelAlign="left" layout="vertical">
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item label="项目模块名称 (artifactId)" name="projectModuleName">
              <a-input v-model:value="projectModuleFormModel.projectModuleName" placeholder="以小写字母组成，可以使用中线分隔" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="项目模块说明 (name)" name="projectModuleNotes">
              <a-input v-model:value="projectModuleFormModel.projectModuleNotes" placeholder="输入项目模块说明" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="项目路径" name="projectPath">
              <a-input v-model:value="projectModuleFormModel.projectPath" placeholder="注意！需要输入绝对路径" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="vue项目路径" name="projectPathVue">
              <a-input v-model:value="projectModuleFormModel.projectPathVue" placeholder="注意！需要输入绝对路径" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="uni项目路径" name="projectPathUni">
              <a-input v-model:value="projectModuleFormModel.projectPathUni" placeholder="注意！需要输入绝对路径" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="代码缩进" name="codeCi">
              <a-radio-group v-model:value="projectModuleFormModel.codeCi">
                <a-radio-button value="ci_2_space">2个空格</a-radio-button>
                <a-radio-button value="ci_4_space">4个空格</a-radio-button>
                <a-radio-button value="ci_tab">制表符</a-radio-button>
              </a-radio-group>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="排序权重" name="weightOrder">
              <a-input-number v-model:value="projectModuleFormModel.weightOrder" :style="{ ...coreStyle.width(200) }" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="数据库类型" name="dbType">
              <a-radio-group v-model:value="projectModuleFormModel.dbType">
                <a-radio-button value="mysql">MySQL</a-radio-button>
                <a-radio-button value="oracle">Oracle</a-radio-button>
              </a-radio-group>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="数据库名" name="dbName" v-if="projectModuleFormModel.dbType === 'mysql'">
              <a-input v-model:value="projectModuleFormModel.dbName" placeholder="输入数据库名" />
            </a-form-item>
            <a-form-item label="服务名" name="serverName" v-if="projectModuleFormModel.dbType === 'oracle'">
              <a-input v-model:value="projectModuleFormModel.serverName" placeholder="输入服务名" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="IP地址" name="ipAddress">
              <a-input v-model:value="projectModuleFormModel.ipAddress" placeholder="输入IP地址" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="端口" name="port">
              <a-input v-model:value="projectModuleFormModel.port" placeholder="输入端口" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="用户名" name="username">
              <a-input v-model:value="projectModuleFormModel.username" placeholder="输入用户名" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="密码" name="password">
              <a-input v-model:value="projectModuleFormModel.password" placeholder="输入密码" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button @click="projectModuleFormReset">
            <CoreIcon icon="ant-design:clear-outlined" :size="14" />
            重置
          </a-button>
          <a-button type="primary" ghost :loading="projectModuleFormSaveSubmitting" @click="projectModuleFormNextAdd">
            <CoreIcon icon="tabler:player-track-next" :size="14" />
            继续录入
          </a-button>
          <a-button type="primary" ghost :loading="projectModuleFormSaveSubmitting" @click="projectModuleFormSave">
            <CoreIcon icon="ant-design:save-outlined" :size="14" />
            保存
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="configureDbTablesEditDrawerVisible"
      title="数据库表配置"
      placement="right"
      :closable="false"
      :width="1120"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
      @close="configureDbTablesEditDrawerOnClose"
    >
      <a-table
        rowKey="key"
        :columns="dbTableConfEditListColumns"
        :data-source="dbTableConfEditListDataSet"
        :pagination="false"
        bordered
        :scroll="{ x: '100%', y: 658 }"
        :style="{ ...coreStyle.marginTop(5) }"
      >
        <template #tableName="{ text, record, index }">
          <a-select v-model:value="dbTableConfEditListDataSet[index].tableName" optionFilterProp="title" show-search allowClear placeholder="请选择" :style="{ ...coreStyle.width('100%') }" @change="(value, option) => dbTableConfTableNameChange(value, option, index)">
            <a-select-option v-for="item in dbAllTablesInfoSelectData" :value="item.dbTableName" :key="item.dbTableName" :title="item.dbTableName + ' - ' + (item.dbTableNameAnnotation || item.dbTableName)">
              {{ item.dbTableName + ' - ' + (item.dbTableNameAnnotation || item.dbTableName) }}
            </a-select-option>
          </a-select>
        </template>
        <template #tableNameAnnotation="{ text, record, index }">
          <a-input v-model:value="dbTableConfEditListDataSet[index].tableNameAnnotation" />
        </template>
        <template #dbTablePrefix="{ text, record, index }">
          <a-input v-model:value="dbTableConfEditListDataSet[index].dbTablePrefix" />
        </template>
        <template #weightOrder="{ text, record, index }">
          <a-input-number v-model:value="dbTableConfEditListDataSet[index].weightOrder" />
        </template>
        <template #action="{ text, record, index }">
          <a @click="dbTableConfDeleteRow(record, index)">
            <CoreIcon icon="fluent:delete-28-regular" color="#ed4014" :size="22" />
          </a>
        </template>
      </a-table>
      <a-button type="dashed" block :style="{ ...coreStyle.marginTop(10) }" @click="dbTableConfAddRow">
        <CoreIcon icon="carbon:add-alt" :size="14" />
        新增
      </a-button>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button type="primary" ghost :loading="dbTableConfEditListDataSetSaveSubmitting" @click="dbTableConfEditListDataSetSave">
            <CoreIcon icon="ant-design:save-outlined" :size="14" />
            保存
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="codeGenerateDrawerVisible"
      title="代码生成"
      placement="right"
      :closable="false"
      :width="720"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 0, 0) }"
    >
      <a-row :gutter="16">
        <a-col :span="12">
          <a-tree :tree-data="projectModuleDbTableConfTreeData" :selectable="false" checkable v-model:checkedKeys="projectModuleDbTableConfTreeCheckedKeys" v-model:expandedKeys="projectModuleDbTableConfTreeExpandedKeys" @check="projectModuleDbTableConfTreeCheckChange">
          </a-tree>
        </a-col>
        <a-col :span="12">
          <a-space direction="vertical" :size="10" :style="{ ...coreStyle.width('100%') }">
            <a-descriptions size="small" :column="1" layout="vertical" bordered>
              <a-descriptions-item label="项目路径" :span="1">{{ codeGenerateDrawerProjectModuleModel.projectPath }}</a-descriptions-item>
            </a-descriptions>
            <a-card title="后台项目模块生成" size="small">
              <a-space>
                <a-button type="primary" size="small" ghost @click="toGenerateProjectModule">
                  <CoreIcon icon="whh:lightning" :size="13" />
                  生成
                </a-button>
              </a-space>
            </a-card>
            <a-card title="java 后台代码生成" size="small">
              <a-space>
                <a-button type="primary" size="small" ghost @click="toGenerate">
                  <CoreIcon icon="whh:lightning" :size="13" />
                  生成
                </a-button>
                <a-button type="dashed" size="small" danger @click="toDelProCodeFiles">
                  <CoreIcon icon="fluent:delete-28-regular" :size="13" />
                  删除代码
                </a-button>
                <a-button type="dashed" size="small" @click="toRegenerate">
                  <CoreIcon icon="cil:aperture" :size="13" />
                  重新生成
                </a-button>
              </a-space>
            </a-card>
            <a-descriptions size="small" :column="1" layout="vertical" bordered>
              <a-descriptions-item label="vue项目路径" :span="1">{{ codeGenerateDrawerProjectModuleModel.projectPathVue }}</a-descriptions-item>
            </a-descriptions>
            <a-card title="vue3 前端代码生成" size="small">
              <a-space>
                <a-button type="primary" size="small" ghost @click="toGenerateVue">
                  <CoreIcon icon="whh:lightning" :size="13" />
                  生成
                </a-button>
                <a-button type="dashed" size="small" danger @click="toDelVueCodeFiles">
                  <CoreIcon icon="fluent:delete-28-regular" :size="13" />
                  删除代码
                </a-button>
                <a-button type="dashed" size="small" @click="toRegenerateVue">
                  <CoreIcon icon="cil:aperture" :size="13" />
                  重新生成
                </a-button>
              </a-space>
            </a-card>
            <a-descriptions size="small" :column="1" layout="vertical" bordered>
              <a-descriptions-item label="uni项目路径" :span="1">{{ codeGenerateDrawerProjectModuleModel.projectPathUni }}</a-descriptions-item>
            </a-descriptions>
            <a-card title="uni 前端代码生成" size="small">
              <a-space>
                <a-button type="primary" size="small" ghost @click="">
                  <CoreIcon icon="whh:lightning" :size="13" />
                  生成
                </a-button>
                <a-button type="dashed" size="small" danger @click="">
                  <CoreIcon icon="fluent:delete-28-regular" :size="13" />
                  删除代码
                </a-button>
                <a-button type="dashed" size="small" @click="">
                  <CoreIcon icon="cil:aperture" :size="13" />
                  重新生成
                </a-button>
              </a-space>
            </a-card>
          </a-space>
        </a-col>
      </a-row>
    </a-drawer>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component'
import { mapStores } from 'pinia'
import { useCoreToolStore } from '/@/store/modules/cn/core/tool/coreToolStore'
import type { Res } from '/@/tool/Res'
import type { Param } from '/@/tool/Param'
import type { CoreTree } from '/@/tool/CoreTree'

import { ScrollContainer } from '/@/components/Container/index'

import { useProjectModuleStore } from '/@/store/modules/cn/core/coding/projectModuleStore'

import type { ProjectModule } from '/@/model/cn/core/coding/eo/ProjectModule'
import { toProjectModule } from '/@/model/cn/core/coding/eo/ProjectModule'

import type { ProjectModuleQO } from '/@/model/cn/core/coding/qo/ProjectModuleQO'
import { toProjectModuleQO } from '/@/model/cn/core/coding/qo/ProjectModuleQO'

import type { ProjectModuleVO } from '/@/model/cn/core/coding/vo/ProjectModuleVO'

import { useCodingStore } from '/@/store/modules/cn/core/coding/codingStore'

import type { DbTablesInfoVO } from '/@/model/cn/core/coding/vo/DbTablesInfoVO'

import { useDbTableConfStore } from '/@/store/modules/cn/core/coding/dbTableConfStore'

import type { DbTableConf } from '/@/model/cn/core/coding/eo/DbTableConf'
import { toDbTableConf } from '/@/model/cn/core/coding/eo/DbTableConf'

import type { DbTableConfQO } from '/@/model/cn/core/coding/qo/DbTableConfQO'

@Options({
  name: 'CoreCodingPage',
  components: {
    ScrollContainer,
  },
  computed: {
    ...mapStores(useCoreToolStore, useProjectModuleStore, useCodingStore, useDbTableConfStore),
  },
})
export default class CoreCodingPage extends Vue {

  projectModuleListDataSet: Array<ProjectModuleVO> = []

  projectModuleListGetDataSet() {
    let param: ProjectModuleQO = toProjectModuleQO()
    param.weightOrderAsc = 'y'
    this.projectModuleStore.queryVo(this.$toParam<ProjectModuleQO>(param)).then((res: Res<ProjectModuleVO>) => {
      if (res.succ) {
        this.projectModuleListDataSet = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  getCodeCiDisplayName(value) {
    if ('ci_2_space' === value) {
      return '2个空格'
    } else if ('ci_4_space' === value) {
      return '4个空格'
    } else if ('ci_tab' === value) {
      return '制表符'
    } else {
      return value
    }
  }

  getDbTypeDisplayName(value) {
    if ('mysql' === value) {
      return 'MySQL'
    } else if ('oracle' === value) {
      return 'Oracle'
    } else {
      return value
    }
  }

  projectModuleFormQuerySubmitting: boolean = false

  projectModuleFormQueryModel: ProjectModuleQO = toProjectModuleQO()

  projectModuleFormQuerySearch() {
    this.projectModuleFormQuerySubmitting = true
    this.projectModuleStore.queryVo(this.$toParam<ProjectModuleQO>(this.$toSearch({ ...this.projectModuleFormQueryModel }))).then((res: Res<ProjectModuleVO>) => {
      if (res.succ) {
        this.projectModuleListDataSet = [ ...res.data ]
        setTimeout(() => { this.projectModuleFormQuerySubmitting = false }, 1000)
      } else {
        this.$message.error(res.msg, 6)
        setTimeout(() => { this.projectModuleFormQuerySubmitting = false }, 1000)
      }
    }).catch(err => {
      setTimeout(() => { this.projectModuleFormQuerySubmitting = false }, 1000)
    })
  }

  projectModuleEditDrawerVisible: boolean = false

  projectModuleFormSaveSubmitting: boolean = false

  projectModuleFormSaveContinue: boolean = false

  projectModuleFormModel: ProjectModule = toProjectModule()

  projectModuleFormRules: object = {
    projectModuleName: [
      { type: 'string', required: true, message: '项目模块名称不能为空！', trigger: ['change', 'blur'] },
    ],
    projectModuleNotes: [
      { type: 'string', required: true, message: '项目模块说明不能为空！', trigger: ['change', 'blur'] },
    ],
    codeCi: [
      { type: 'string', required: true, message: '代码缩进不能为空！', trigger: ['change', 'blur'] },
    ],
    projectPath: [
      { type: 'string', required: true, message: '项目路径不能为空！', trigger: ['change', 'blur'] },
    ],
    projectPathVue: [
      { type: 'string', required: true, message: 'vue项目路径不能为空！', trigger: ['change', 'blur'] },
    ],
    projectPathUni: [
      { type: 'string', required: true, message: 'uni项目路径不能为空！', trigger: ['change', 'blur'] },
    ],
    dbType: [
      { type: 'string', required: true, message: '数据库类型不能为空！', trigger: ['change', 'blur'] },
    ],
    ipAddress: [
      { type: 'string', required: true, message: 'IP地址不能为空！', trigger: ['change', 'blur'] },
    ],
    port: [
      { type: 'string', required: true, message: '端口不能为空！', trigger: ['change', 'blur'] },
    ],
    serverName: [
      { type: 'string', required: true, message: '服务名不能为空！', trigger: ['change', 'blur'] },
    ],
    dbName: [
      { type: 'string', required: true, message: '数据库名不能为空！', trigger: ['change', 'blur'] },
    ],
    username: [
      { type: 'string', required: true, message: '用户名不能为空！', trigger: ['change', 'blur'] },
    ],
    password: [
      { type: 'string', required: true, message: '密码不能为空！', trigger: ['change', 'blur'] },
    ],
  }

  projectModuleFormReset() {
    if (this.$refs.projectModuleFormRef) {
      this.$refs.projectModuleFormRef.resetFields()
      this.projectModuleFormModel = toProjectModule()
    }
  }

  projectModuleFormSubmit() {
    this.projectModuleFormSaveSubmitting = true
    this.$refs.projectModuleFormRef.validate().then(() => {
      if (this.projectModuleFormModel.id) {
        this.projectModuleStore.update(this.$toParam<ProjectModule>(this.projectModuleFormModel)).then((res: Res<ProjectModule>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.projectModuleFormReset()
            this.projectModuleFormSaveSubmitting = false
            if (!this.projectModuleFormSaveContinue) {
              this.projectModuleEditDrawerVisible = false
              this.projectModuleListGetDataSet()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.projectModuleFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.projectModuleFormSaveSubmitting = false }, 3000)
        })
      } else {
        this.projectModuleStore.save(this.$toParam<ProjectModule>(this.projectModuleFormModel)).then((res: Res<ProjectModule>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.projectModuleFormReset()
            this.projectModuleFormSaveSubmitting = false
            if (!this.projectModuleFormSaveContinue) {
              this.projectModuleEditDrawerVisible = false
              this.projectModuleListGetDataSet()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.projectModuleFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.projectModuleFormSaveSubmitting = false }, 3000)
        })
      }
    }).catch(() => {
      setTimeout(() => { this.projectModuleFormSaveSubmitting = false }, 3000)
    })
  }

  projectModuleFormSave() {
    this.projectModuleFormSaveContinue = false
    this.projectModuleFormSubmit()
  }

  projectModuleFormNextAdd() {
    this.projectModuleFormSaveContinue = true
    this.projectModuleFormSubmit()
  }

  projectModuleAdd() {
    if (this.projectModuleFormModel.id) {
      this.projectModuleFormReset()
    }
    this.projectModuleEditDrawerVisible = true
  }

  projectModuleUpdate(value) {
    this.projectModuleStore.pk(this.$toParam<ProjectModuleQO>({ id: value } as ProjectModuleQO)).then((res: Res<ProjectModule>) => {
      if (res.succ) {
        this.projectModuleFormReset()
        this.projectModuleFormModel = { ...res.data[0] }
        this.projectModuleEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  projectModuleEditDrawerOnClose() {
    if (this.projectModuleFormSaveContinue) {
      this.projectModuleFormSaveContinue = false
      this.projectModuleListGetDataSet()
    }
  }

  projectModuleDelete(value) {
    this.projectModuleStore.remove(this.$toParam<ProjectModule>({ id: value } as ProjectModule)).then((res: Res<ProjectModule>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
        this.projectModuleListGetDataSet()
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  toAddDataSource(value) {
    this.codingStore.addDataSource(this.$toParam<ProjectModule>({ id: value } as ProjectModule)).then((res: Res<ProjectModule>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  currentProjectModuleId: string = ''

  configureDbTablesEditDrawerVisible: boolean = false

  dbAllTablesInfoSelectData: Array<DbTablesInfoVO> = []

  dbTableConfEditListDataSet: Array<DbTableConf> = []

  dbTableConfEditListColumns: Array<object> = [
    {
      title: '表名',
      dataIndex: 'tableName',
      align: 'center',
      slots: { customRender: 'tableName' },
    },
    {
      title: '表名注释',
      dataIndex: 'tableNameAnnotation',
      align: 'center',
      slots: { customRender: 'tableNameAnnotation' },
      width: 270,
    },
    {
      title: '数据库表前缀',
      dataIndex: 'dbTablePrefix',
      align: 'center',
      slots: { customRender: 'dbTablePrefix' },
      width: 150,
    },
    {
      title: '排序权重',
      dataIndex: 'weightOrder',
      align: 'center',
      slots: { customRender: 'weightOrder' },
      width: 150,
    },
    {
      title: '操作',
      key: 'action',
      align: 'center',
      slots: { customRender: 'action' },
      width: 65,
    },
  ]

  dbTableConfEditListDataSetSaveSubmitting: boolean = false

  configureDbTables(value) {
    this.currentProjectModuleId = value
    this.codingStore.getDbAllTablesInfo(this.$toParam<ProjectModule>({ id: this.currentProjectModuleId } as ProjectModule)).then((res: Res<DbTablesInfoVO>) => {
      if (res.succ) {
        this.dbAllTablesInfoSelectData = [ ...res.data ]
        this.dbTableConfStore.query(this.$toParam<DbTableConfQO>({ fkid: this.currentProjectModuleId, weightOrderAsc: 'y' } as DbTableConfQO)).then((res: Res<DbTableConf>) => {
          if (res.succ) {
            this.dbTableConfEditListDataSet = res.data.map((eo: DbTableConf) => {
              eo.key = this.$getUUID32()
              return eo
            })
            this.configureDbTablesEditDrawerVisible = true
          } else {
            this.$message.error(res.msg, 6)
          }
        })
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  dbTableConfAddRow() {
    let param: DbTableConf = toDbTableConf()
    param.fkid = this.currentProjectModuleId
    param.weightOrder = this.dbTableConfEditListDataSet.length + 1
    param.key = this.$getUUID32()
    this.dbTableConfEditListDataSet.push(param)
  }

  dbTableConfDeleteRow(record, index) {
    if (record.id) {
      this.dbTableConfStore.remove(this.$toParam<DbTableConf>(record)).then((res: Res<DbTableConf>) => {
        if (res.succ) {
          this.$message.success(res.msg, 3)
          this.dbTableConfEditListDataSet.splice(index, 1)
        } else {
          this.$message.error(res.msg, 6)
        }
      })
    } else {
      this.$message.success('数据库表配置信息删除成功！', 3)
      this.dbTableConfEditListDataSet.splice(index, 1)
    }
  }

  dbTableConfEditListDataSetSave() {
    this.dbTableConfEditListDataSetSaveSubmitting = true
    if (this.dbTableConfEditListDataSet.length !== 0) {
      this.codingStore.batchSaveOrUpdateDbTablesConf(this.$toParam<DbTableConf>(this.dbTableConfEditListDataSet)).then((res: Res<DbTableConf>) => {
        if (res.succ) {
          this.$message.success(res.msg, 3)
          this.configureDbTablesEditDrawerVisible = false
          setTimeout(() => { this.dbTableConfEditListDataSetSaveSubmitting = false }, 3000)
        } else {
          this.$message.error(res.msg, 6)
          setTimeout(() => { this.dbTableConfEditListDataSetSaveSubmitting = false }, 3000)
        }
      }).catch(err => {
        setTimeout(() => { this.dbTableConfEditListDataSetSaveSubmitting = false }, 3000)
      })
    } else {
      setTimeout(() => { this.dbTableConfEditListDataSetSaveSubmitting = false }, 3000)
      this.configureDbTablesEditDrawerVisible = false
    }
  }

  configureDbTablesEditDrawerOnClose() {
    this.configureDbTablesEditDrawerVisible = true
    if (this.dbTableConfEditListDataSet.length !== 0) {
      this.codingStore.batchSaveOrUpdateDbTablesConf(this.$toParam<DbTableConf>(this.dbTableConfEditListDataSet)).then((res: Res<DbTableConf>) => {
        if (res.succ) {
          this.$message.success(res.msg, 3)
          this.configureDbTablesEditDrawerVisible = false
        } else {
          this.$message.error(res.msg, 6)
        }
      })
    } else {
      this.configureDbTablesEditDrawerVisible = false
    }
  }

  dbTableConfTableNameChange(value, option, index) {
    this.dbTableConfEditListDataSet[index].tableName = value
    this.dbTableConfEditListDataSet[index].tableNameAnnotation = option.children[0].children.substring(option.children[0].children.indexOf('-') + 2)
    this.dbTableConfEditListDataSet[index].dbTablePrefix = value.substring(0, value.indexOf('_') + 1)
  }

  codeGenerateDrawerVisible: boolean = false

  projectModuleDbTableConfTreeData: Array<CoreTree<any>> = []

  projectModuleDbTableConfTreeCheckedKeys: string[] = []

  projectModuleDbTableConfTreeHalfCheckedKeys: string[] = []

  projectModuleDbTableConfTreeExpandedKeys: string[] = []

  codeGenerateDrawerProjectModuleModel: ProjectModule = toProjectModule()

  codeGenerate(value) {
    this.currentProjectModuleId = value
    this.codingStore.getProjectModuleDbTableConfTreeData(this.$toParam<ProjectModule>({ id: this.currentProjectModuleId } as ProjectModule)).then((res: Res<CoreTree<any>>) => {
      if (res.succ) {
        this.projectModuleDbTableConfTreeData = [ ...res.data ]
        this.projectModuleStore.pk(this.$toParam<ProjectModuleQO>({ id: this.currentProjectModuleId } as ProjectModuleQO)).then((res: Res<ProjectModule>) => {
          if (res.succ) {
            this.codeGenerateDrawerProjectModuleModel = { ...res.data[0] }
            this.projectModuleDbTableConfTreeCheckedKeys = []
            this.projectModuleDbTableConfTreeHalfCheckedKeys = []
            this.projectModuleDbTableConfTreeExpandedKeys = [ this.currentProjectModuleId ]
            this.codeGenerateDrawerVisible = true
          } else {
            this.$message.error(res.msg, 6)
          }
        })
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  projectModuleDbTableConfTreeCheckChange(checkedKeys, {halfCheckedKeys}) {
    this.projectModuleDbTableConfTreeHalfCheckedKeys = [ ...halfCheckedKeys ]
  }

  toGenerateProjectModule() {
    this.codingStore.generateProjectModule(this.$toParam<ProjectModule>({ id: this.currentProjectModuleId } as ProjectModule)).then((res: Res<ProjectModule>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  toGenerate() {
    let checkedTkeys: string[] = []
    this.$getTreeTkeys(this.projectModuleDbTableConfTreeData[0], ['DbTableConf'], [ ...new Set([ ...this.projectModuleDbTableConfTreeCheckedKeys, ...this.projectModuleDbTableConfTreeHalfCheckedKeys ]) ], checkedTkeys)
    let param: Array<DbTableConf> = []
    checkedTkeys.forEach((eo: string) => {
      param.push({ id: eo } as DbTableConf)
    })
    this.codingStore.generate(this.$toParam<DbTableConf>(param)).then((res: Res<DbTableConf>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  toDelProCodeFiles() {
    let checkedTkeys: string[] = []
    this.$getTreeTkeys(this.projectModuleDbTableConfTreeData[0], ['DbTableConf'], [ ...new Set([ ...this.projectModuleDbTableConfTreeCheckedKeys, ...this.projectModuleDbTableConfTreeHalfCheckedKeys ]) ], checkedTkeys)
    let param: Array<DbTableConf> = []
    checkedTkeys.forEach((eo: string) => {
      param.push({ id: eo } as DbTableConf)
    })
    this.codingStore.delProCodeFiles(this.$toParam<DbTableConf>(param)).then((res: Res<DbTableConf>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  toRegenerate() {
    let checkedTkeys: string[] = []
    this.$getTreeTkeys(this.projectModuleDbTableConfTreeData[0], ['DbTableConf'], [ ...new Set([ ...this.projectModuleDbTableConfTreeCheckedKeys, ...this.projectModuleDbTableConfTreeHalfCheckedKeys ]) ], checkedTkeys)
    let param: Array<DbTableConf> = []
    checkedTkeys.forEach((eo: string) => {
      param.push({ id: eo } as DbTableConf)
    })
    this.codingStore.regenerate(this.$toParam<DbTableConf>(param)).then((res: Res<DbTableConf>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  toGenerateVue() {
    let checkedTkeys: string[] = []
    this.$getTreeTkeys(this.projectModuleDbTableConfTreeData[0], ['DbTableConf'], [ ...new Set([ ...this.projectModuleDbTableConfTreeCheckedKeys, ...this.projectModuleDbTableConfTreeHalfCheckedKeys ]) ], checkedTkeys)
    let param: Array<DbTableConf> = []
    checkedTkeys.forEach((eo: string) => {
      param.push({ id: eo } as DbTableConf)
    })
    this.codingStore.generateVue(this.$toParam<DbTableConf>(param)).then((res: Res<DbTableConf>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  toDelVueCodeFiles() {
    let checkedTkeys: string[] = []
    this.$getTreeTkeys(this.projectModuleDbTableConfTreeData[0], ['DbTableConf'], [ ...new Set([ ...this.projectModuleDbTableConfTreeCheckedKeys, ...this.projectModuleDbTableConfTreeHalfCheckedKeys ]) ], checkedTkeys)
    let param: Array<DbTableConf> = []
    checkedTkeys.forEach((eo: string) => {
      param.push({ id: eo } as DbTableConf)
    })
    this.codingStore.delVueCodeFiles(this.$toParam<DbTableConf>(param)).then((res: Res<DbTableConf>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  toRegenerateVue() {
    let checkedTkeys: string[] = []
    this.$getTreeTkeys(this.projectModuleDbTableConfTreeData[0], ['DbTableConf'], [ ...new Set([ ...this.projectModuleDbTableConfTreeCheckedKeys, ...this.projectModuleDbTableConfTreeHalfCheckedKeys ]) ], checkedTkeys)
    let param: Array<DbTableConf> = []
    checkedTkeys.forEach((eo: string) => {
      param.push({ id: eo } as DbTableConf)
    })
    this.codingStore.regenerateVue(this.$toParam<DbTableConf>(param)).then((res: Res<DbTableConf>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  mounted() {
    this.projectModuleListGetDataSet()
    this.projectModuleFormQueryModel.weightOrderAsc = 'y'
  }

}
</script>

<style scoped lang="less">

:deep(.ant-card-actions) {
  background: #ffffff;
}

</style>
